// 基于准备好的dom，初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));//定义折线图显示的位置对象
 option = {
     title: {
         text: '学院到课率横向比'
     },
 legend: {
         data:[]
     },
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
        type: 'value',
                axisLabel: {
                    formatter: '{value} %'
                }
    },
    series: [ {
                         name:'邮件营销',
                         type:'line',
                         stack: '到课率',
                         data:[120, 132, 101, 134, 90, 230, 210]
                     },
                     {
                         name:'联盟广告',
                         type:'line',
                         stack: '到课率',
                         data:[220, 182, 191, 234, 290, 330, 310]
                     },]
};


$(function () {
    $.ajax({
        url: baseURL + "/college/getAllStudentCollege2",
        data: {},
        success: function (r) {
            $("#College").append("<option value=''>请选择学院</option>");
            for (i = 0; i < r.list.length; i++) {
                var tname = r.list[i].collegeName;
                var tid = r.list[i].collegeName;
                $("#College").append("<option value='" + tid + "'>" + tname + "</option>");
            }
        }
    });
    $.ajax({
        url: baseURL + "AttendenceCharts/getAttendanceCollegeHorizontalComparison",
        data: {
            "collegeName":$("#College").val(),
            "startDate":$("#startDate").val(),
            "endDate":$("#endDate").val()
        },
        success: function (r) {
        option.series={
            type:'line',
            data:r.r.data
        };
        option.xAxis= {
        type: 'category',
        data:r.r.xAxis
    };
        option.legend.data=r.r.legend;
        myChart.setOption(option);
        }
    });
    $("#option").change(function () {
        document.getElementById("CollegeSelect").style.display="none";
        $("#multiSelect").empty();
        var option = $("#option").val();
        if (option == 'college') {
            $.ajax({
                url: baseURL + "/college/getAllStudentCollege2",
                success: function (r) {
                    $("#multiSelect").empty();
                    $("#multiSelect").append("请选择学院<br/>");
                    for (i = 0; i < r.list.length; i++) {
                        var tname = r.list[i].collegeName;
                        $("#multiSelect").append("<label><input type=\"checkbox\" name=\"multiSelect\" value=\"" + tname + "\" />" + tname + "</label>&nbsp&nbsp");
                    }
                }
            });
        }else if(option == 'class'){
            document.getElementById("CollegeSelect").style.display=""
        }
    });

})
function query()
{
var obj=document.getElementsByName('multiSelect');
    var s='';
    for(var i=0; i<obj.length; i++){
        if(obj[i].checked)
            s+=obj[i].value+','; //如果选中，将value添加到变量s中
    }
   $.ajax({
                url: baseURL + "AttendenceCharts/getAttendanceCollegeHorizontalComparison",
                data: {
                    "collegeName":s,
                    "startDate":$("#startDate").val(),
                    "endDate":$("#endDate").val()
                },
                success: function (r) {
                option.series=r.r.data;
                option.xAxis= {
                type: 'category',
                data:r.r.xAxis
            };
            option.legend.data=r.r.legend;
                myChart.setOption(option);
                }
            });
}
function setDate(id) {
    var Nowdate=new Date();
    M=Number(Nowdate.getMonth())+1;
    var today=Nowdate.getFullYear()+"-"+fix(M)+"-"+fix(Nowdate.getDate());
    if(id=='1'){
        $("#startDate").val(today);
        $("#endDate").val(today);
    }else if(id=='2'){
        $("#startDate").val(showWeekFirstDay());
        $("#endDate").val(today);
    }if(id=='3'){
        $("#startDate").val(showMonthFirstDay());
        $("#endDate").val(today);
    }
    query()
}
function showWeekFirstDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    M=Number(WeekFirstDay.getMonth())+1
    return WeekFirstDay.getFullYear()+"-"+fix(M)+"-"+fix(WeekFirstDay.getDate());
}
function showWeekLastDay()
{
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
    M=Number(WeekLastDay.getMonth())+1
    return WeekLastDay.getFullYear()+"-"+M+"-"+WeekLastDay.getDate();
}
function showMonthFirstDay()
{
    var Nowdate=new Date();
    var MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1);
    M=Number(MonthFirstDay.getMonth())+1
    return MonthFirstDay.getFullYear()+"-"+fix(M)+"-"+fix(MonthFirstDay.getDate());
}
function fix(num){
    if(num<10)
        return "0"+num;
    else return ""+num;
}
function showMonthLastDay()
{
    var Nowdate=new Date();
    var MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1,1);
    var MonthLastDay=new Date(MonthNextFirstDay-86400000);
    M=Number(MonthLastDay.getMonth())+1
    return MonthLastDay.getFullYear()+"-"+M+"-"+MonthLastDay.getDate();
}